package com.stylefeng.guns.modular.system.controller.server;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.common.annotion.BussinessLog;
import com.stylefeng.guns.common.constant.factory.PageFactory;
import com.stylefeng.guns.common.persistence.dao.ProjectConfigMapper;
import com.stylefeng.guns.common.persistence.dao.ProjectTableMapper;
import com.stylefeng.guns.common.persistence.dao.ProjectTablePropertyMapper;
import com.stylefeng.guns.common.persistence.model.Documents;
import com.stylefeng.guns.common.persistence.model.ProjectConfig;
import com.stylefeng.guns.common.persistence.model.ProjectTable;
import com.stylefeng.guns.common.persistence.model.ProjectTableProperty;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.base.tips.Tip;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.modular.system.dao.ProjectConfigDao;
import com.stylefeng.guns.modular.system.dao.ProjectTableDao;
import com.stylefeng.guns.modular.system.dao.ProjectTablePropertyDao;
import com.stylefeng.guns.modular.system.service.IDocumentsService;
import com.stylefeng.guns.modular.system.service.IProjectTableService;
import com.stylefeng.guns.modular.system.warpper.ProjectTablePropertyWarpper;
import com.stylefeng.guns.modular.system.warpper.ProjectTableWarpper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author lmshe
 * @create 2018-07-04 14:12
 * @desc
 */
@Controller
@RequestMapping("/projectTable")
public class ProjectTableController extends BaseController {
    private static String PREFIX = "/system/projectTable/";

    @Resource
    private ProjectTableDao projectTableDao;

    @Resource
    private ProjectTableMapper projectTableMapper;

    @Resource
    private ProjectConfigDao projectConfigDao;

    @Resource
    private ProjectConfigMapper projectConfigMapper;

    @Resource
    private ProjectTablePropertyDao projectTablePropertyDao;

    @Resource
    private ProjectTablePropertyMapper projectTablePropertyMapper;

    @Resource
    private IProjectTableService projectTableService;

    /**
     * 跳转列表页面
     * @return
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "project_table.html";
    }

    @RequestMapping("/list")
    @ResponseBody
    public Object list() {
        Page<Map<String, Object>> page = new PageFactory<Map<String, Object>>().defaultPage();
        List<Map<String, Object>> result = projectTableDao.getProjectTableList(page, page.getOrderByField(), page.isAsc());
        page.setRecords((List<Map<String, Object>>) new ProjectTableWarpper(result).warp());
        return super.packForBT(page);
    }

    /**
     * 跳转到添加页面
     * @return
     */
    @RequestMapping("/project_table_add")
    public String projectTableAdd(Model model) {
        List<ProjectConfig> projectConfigList = this.projectConfigMapper.selectList(new EntityWrapper<>());
        model.addAttribute("projectConfigList", projectConfigList);
        return PREFIX + "project_table_add.html";
    }

    /**
     * 添加
     * @param model
     * @return
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @BussinessLog(value = "新增项目虚拟表",key = "project_table")
    public Object add(ProjectTable model) {
        model.insert();
        return super.SUCCESS_TIP;
    }

    /**
     * 跳转到修改页面
     */
    @RequestMapping("/project_table_update/{id}")
    public String projectTableUpdate(@PathVariable Integer id, Model model) {
        ProjectTable  projectTable = this.projectTableMapper.selectById(id);
        model.addAttribute("projectTable",projectTable);
        List<ProjectConfig> projectConfigList = this.projectConfigMapper.selectList(new EntityWrapper<>());
        model.addAttribute("projectConfigList", projectConfigList);
        LogObjectHolder.me().set(projectTable);
        return PREFIX + "project_table_edit.html";
    }

    /**
     * 修改
     * @param model
     * @return
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @BussinessLog(value = "修改项目虚拟表",key = "project_table")
    public Object update(ProjectTable model) {
        this.projectTableMapper.updateById(model);
        return super.SUCCESS_TIP;
    }

    /**
     * 跳转到属性列表页面
     */
    @RequestMapping("/table_property_edit/{id}")
    public String tablePropertyEdit(@PathVariable Integer id, Model model) {
        ProjectTable  projectTable = this.projectTableMapper.selectById(id);
        model.addAttribute("projectTable",projectTable);
        model.addAttribute("projectTablePropertyList", new ProjectTablePropertyWarpper(projectTablePropertyDao.getProjectTablePropertyListByTableId(id)).warp());
        LogObjectHolder.me().set(projectTable);
        return PREFIX + "table_property_edit.html";
    }

    @RequestMapping(value = "/edit_property")
    @ResponseBody
    @BussinessLog(value = "修改属性字段", key = "propertyId")
    public Object online(@RequestParam Integer id,@RequestParam Integer propertyId,@RequestParam String CNName,
                         @RequestParam String ENName,@RequestParam Integer length,@RequestParam Integer isNull,
                         @RequestParam String dbType,@RequestParam String codeType) {
        ProjectTableProperty model = this.projectTablePropertyMapper.selectById(propertyId);
        if(model == null){
            model = new ProjectTableProperty();
        }
        model.setCNName(CNName);
        model.setENName(ENName);
        model.setLength(length);
        model.setIsNull(isNull);
        model.setDbType(dbType);
        model.setCodeType(codeType);
        if(propertyId == 0){
            model.setProjectTableId(id);
            this.projectTablePropertyMapper.insert(model);
        }else{
            this.projectTablePropertyMapper.updateById(model);
        }
        return super.SUCCESS_TIP;
    }

    @RequestMapping(value = "/generator_code")
    @ResponseBody
    @BussinessLog(value = "代码生成", key = "projectTableId")
    public Object generatorCode(@RequestParam Integer projectTableId) {
        String result = projectTableService.generatorCode(projectTablePropertyDao.getListByTableId(projectTableId),
                this.projectTableMapper.selectById(projectTableId));
        return super.SUCCESS_TIP;
    }
}
